package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;
/**
 * 待召开会议
 * @author 汪恭泽
 *
 */
public class HeldtoMeetingTabPanel extends ContentPanel {
	private ColumnModel cm;
	private GridCellRenderer<Meeting> date;

	public HeldtoMeetingTabPanel(ListStore<Meeting> store) {
		setLayout(new FitLayout());
		setHeaderVisible(false);
		setFrame(true);

		final DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");

		date = new GridCellRenderer<Meeting>() {
			@Override
			public Object render(Meeting model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<Meeting> store, Grid<Meeting> grid) {
				Integer cancelTAG = (Integer) model.getCancelTAG();
				Date val = (Date) model.get(property);
				if (cancelTAG != null && cancelTAG == 1) {
					return "<span style='color: red;text-decoration: line-through;'>"
							+ dtf.format(val)
							+ "</span>&nbsp;&nbsp;<span style='color: red;font-weight: bold;'>已取消</span>";
				} else {
					return "<span>" + dtf.format(val) + "</span>";
				}
			}
		};

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("originatorName");
		column.setHeader("发起人");
		column.setWidth(100);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("chairName");
		column.setHeader("主席");
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("主题");
		column.setWidth(400);
		configs.add(column);

		column = new ColumnConfig("startDate", "会议时间", 175);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(date);
		configs.add(column);

		cm = new ColumnModel(configs);

		Grid<Meeting> grid = new Grid<Meeting>(store, cm);
		grid.setAutoExpandColumn("subject");
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		grid.setStateful(true);

		grid.setStateful(true);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<Meeting>>() {
					public void handleEvent(SelectionChangedEvent<Meeting> be) {
						if (be.getSelection().size() > 0) {
							Meeting meeting = be.getSelection().get(0);
							Dispatcher.forwardEvent(AppEvents.ToMeetingContent,
									meeting.getId());
						}
					}
				});

		ContentPanel cp = new ContentPanel();
		cp.setBodyBorder(false);
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.setHeight(205);
		cp.add(grid);

		VerticalPanel vp = new VerticalPanel();
		vp.setLayout(new FlowLayout(5));
		vp.setBorders(false);
		vp.setHeight(250);

		vp.add(createTitle("待召开的会议"));

		vp.add(cp);
		add(vp);
	}

	private Text createTitle(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb.append("<p style='font-size:22px;font-weight: bold;color:#0060A0;'>"
				+ str + "</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		return title;
	}
}
